import * as apis from '../services/example'

export default {
    namespace: "indexTest",
    state: {
        name: "Starbug",
        whoami: "loser",
        cnodeDataList: [],
    },
    reducers: {
        /**
         * @param state 当前model的state
         * @param payload 调用该方法时,调用者传入的参数
         */
        setName(state, payload) {
            //直接修改,state会改变,但是页面上渲染的数据不会更新
            // state.name = payload.data.name;

            //生成一个新的对象(新的地址,返回给react,就会更新页面上的数据)
            let _state = JSON.parse(JSON.stringify(state));
            _state.name = payload.data.name;

            console.log(state)
            return _state;
        },
        setCnodeDataList(state, payload) {
            let _state = JSON.parse(JSON.stringify(state));
            _state.cnodeDataList = payload.data;
            return _state;
        },
        testPath(state, payload) {
            console.log('testPathppppp')
            return state;
        }
    },
    effects: {
        //es6 generator
        * setNameAsync({data}, {put, call}) {
            yield put({
                type: "setName",
                data: {
                    name: data.name
                }
            })
        },
        * testCnode({payload}, {put, call}) {
            let result = yield call(apis.testCnode);
            if (result.data.success) {
                console.log(result.data.data)
                yield put({
                    type: "setCnodeDataList",
                    data: result.data.data,
                })
            }
        },
    },
    subscriptions: {
        //启动时,自动执行里面的方法,这里启动时,判断路由是不是user,时则执行dispatch中的代码
        haha({dispatch, history}) {
            history.listen(({pathname}) => {
                if (pathname === "/user") {
                    console.log('userrrrrr', pathname)
                    dispatch({
                        type: "testPath"
                    })
                }
            })
        }
    }
}
